<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of VideoController
 *
 * @author Administrator
 */
class VideoController extends Controller {

    //put your code here
    public function actionIndex() {
        $model = new Video();

        $this->render('index', array('model' => $model));
    }

    public function actionAdd() {
        $model = new Video();
        //$this->performAjaxValidation($model);
        $request = Yii::app()->request;
        if ($request->getIsPostRequest()) {
            $form = $request->getPost('Video');
            $model->attributes = $form;
            if ($model->video_allow_play == 1) {
                // if( $request->getPost('video_player_alias'))
            }
            $transaction = $model->getDbConnection()->beginTransaction();
            try {
                if ($model->save()) {
                    $video_id = $model->getPrimaryKey();
                    if (count($form['video_category']) <= 0) {
                        throw new Exception(Yii::t('error', '请选择分类'));
                    } else {

                        foreach ($form['video_category'] as $categoryId) {
                            $category = new VideoCategorys();
                            $data = array();
                            $data['category_id'] = $categoryId;
                            $data['video_id'] = $video_id;
                            $category->attributes = $data;

                            if (!$category->save()) {
                                echo CHtml::errorSummary($category);
                                throw new Exception('error');
                            }
                        }
                    }
                    $play = $request->getPost('Play');
                    if (count($play) == 0) { //没有生成路径给出错误提示
                        throw new Exception(Yii::t('error', '还没生成视频文件路径'));
                    } else {
                        for ($i = 0; $i < count($play); $i++) {
                            $VideolFile = new VideolFile();
                            $data = array();
                            $data['file_path'] = $play['file_path'][$i];
                            $data['file_name'] = $play['file_name'][$i];
                            $data['file_sort'] = $i + 1;
                            $data['server_id'] = $model->attributes['video_play_server_id'];
                            $data['video_id'] = $video_id;
                            $data['file_play_mode'] = 'auth';
                            $data['file_mode'] = 'default';
                            $VideolFile->attributes = $data;
                            if (!$VideolFile->save()) {
                                echo CHtml::errorSummary($VideolFile);
                                throw new Exception('error');
                            }
                        }
                    }
                    /* if ($model->attributes['video_allow_play'] == 1) {
                      $play = $request->getPost('Play');
                      if (count($play) == 0) { //没有生成路径给出错误提示
                      throw new Exception(Yii::t('error', '还没生成点播路径'));
                      } else {
                      for ($i = 0; $i < count($play); $i++) {
                      $VideolFile = new VideolFile();
                      $data = array();
                      $data['file_path'] = $play['file_path'][$i];
                      $data['file_name'] = $play['file_name'][$i];
                      $data['file_sort'] = $i + 1;
                      $data['server_id'] = $model->attributes['video_play_server_id'];
                      $data['video_id'] = $video_id;
                      $data['file_play_mode'] = 'auth';
                      $data['file_mode'] = 'default';
                      $VideolFile->attributes = $data;
                      if (!$VideolFile->save()) {
                      echo CHtml::errorSummary($VideolFile);
                      throw new Exception('error');
                      }
                      }
                      }
                      }

                      if ($model->attributes['video_allow_down'] == 1) {
                      $down = $request->getPost('Down');
                      for ($i = 0; $i < count($down); $i++) {
                      $VideolFile = new VideolFile();
                      $data = array();
                      $data['file_path'] = $down['file_path'][$i];
                      $data['file_name'] = $down['file_name'][$i];
                      $data['file_sort'] = $i + 1;
                      $data['server_id'] = $model->attributes['video_down_server_id'];
                      $data['video_id'] = $video_id;
                      $data['file_play_mode'] = 'auth';
                      $data['file_mode'] = 'down';
                      $VideolFile->attributes = $data;
                      if (!$VideolFile->save()) {
                      echo CHtml::errorSummary($VideolFile);
                      throw new Exception('error');
                      }
                      }
                      } */
                } 
                $transaction->commit();
            } catch (Exception $e) {
                $transaction->rollback();
            }
        } else {
            $model->video_region = Config::model()->getConfig('video_default_region');
            $model->video_quality = Config::model()->getConfig('video_default_quality');
            $model->video_dialog = Config::model()->getConfig('video_default_dialog');
            $model->video_allow_play = Config::model()->getConfig('video_default_play');
            $model->video_allow_down = Config::model()->getConfig('video_default_down');
            $model->video_play_money = (int) Config::model()->getConfig('video_play_money');
            $model->video_down_money = (int) Config::model()->getConfig('video_down_money');
        }
        $this->render('add', array('model' => $model));
    }

    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'video-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

    public function actionDel() {
        if (Yii::app()->request->getIsAjaxRequest()) {
            if (Video::model()->remove(Yii::app()->request->getParam('id'))) {
                echo '删除成功';
            } else {
                echo '删除失败';
            }
        }
        Yii::app()->end();
    }

}